package hotel;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.*;

public class FinestraAssociazione extends JFrame implements ActionListener {
	private ResultSet rs = null;
	String camera;
	String query1;
	private JPanel p, p1, p2;
	private int[] rows;
	private JButton riempi;
	private JScrollPane scroll;
	private String[] camereDisponibili;
	private JList lista;
	private JLabel[] clienti;
	private JComboBox<String> box;
	private int numPostiOccupati;

	public FinestraAssociazione(int[] rows) {
		this.rows = rows;
		setSize(400, 400);
		setVisible(true);
		this.setLocation(450,200);
		p = new JPanel();
		p.setLayout(new BorderLayout());
		add(p);
		p1 = new JPanel();
		p1.setLayout(new GridLayout(20, 20, 0, 0));
		p2 = new JPanel();
		clienti = new JLabel[rows.length];
		p1.add(new JLabel("CLIENTE/I:"));
		for (int i = 0; i < rows.length; i++) {
			clienti[i] = new JLabel(GraficaHotel.modelClienti.getValueAt(
					rows[i], 0)
					+ " "
					+ GraficaHotel.modelClienti.getValueAt(rows[i], 1));
			p1.add(clienti[i]);
		}
		p.add(p1, BorderLayout.WEST);
		p.add(p2, BorderLayout.EAST);
		camereDisponibili = new String[GraficaHotel.modelCamere.getRowCount()];
		for (int i = 0; i < camereDisponibili.length; i++) {
			camereDisponibili[i] = (String) GraficaHotel.modelCamere
					.getValueAt(i, 0);
		}
		riempi = new JButton("Riempi");
		riempi.addActionListener(this);
		p.add(riempi, BorderLayout.SOUTH);
		lista = new JList(camereDisponibili);
		scroll = new JScrollPane(lista);
		lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lista.setPreferredSize(new Dimension(100, 200));
		p2.add(new JLabel("CAMERE:"));
		p2.add(scroll);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == riempi) {
			camera = (String) lista.getSelectedValue();
			query1 = "SELECT * FROM CAMERE where numero='" + camera + "'";

			for (int i = 0; i < clienti.length; i++) {
				try {
					rs = GraficaHotel.database.stat.executeQuery(query1);
				} catch (SQLException e2) {
					e2.printStackTrace();
				}
				try {
					if (rs.next())

						try {
							if (!controlloCamera()) {
								JOptionPane
										.showMessageDialog(null,
												"Nessun posto disponibile nella camera selezionata!");

							} else if (controlloCliente(
									(String) GraficaHotel.modelClienti.getValueAt(
											rows[i], 3), i)) {
								rs=GraficaHotel.database.stat.executeQuery("SELECT camera FROM ASSOCIAZIONE where numerodocumento='"+(String) GraficaHotel.modelClienti.getValueAt(
											rows[i], 3)+"'");
								if(rs.next())
								JOptionPane.showMessageDialog(null,
										clienti[i].getText()
												+ " occupa già la camera "
												+ rs.getString(1) + "!");

							} else {
								System.out.println("OK");
								rs = GraficaHotel.database.stat
										.executeQuery(query1);
								System.out.println("dopo resultSet");
								if (rs.next())
									if (rs.getString(6)
											.compareTo("DISPONIBILE") == 0)
										GraficaHotel.database.stat
												.executeUpdate("UPDATE CAMERE SET stato='OCCUPATA', numpostioccupati= numpostioccupati+1 where numero='"
														+ camera + "'");
									else if (rs.getString(6).compareTo(
											"OCCUPATA") == 0)
										GraficaHotel.database.stat
												.executeUpdate("UPDATE CAMERE SET numpostioccupati= numpostioccupati+1 where numero='"
														+ camera + "'");
								GraficaHotel.database
										.query("INSERT INTO ASSOCIAZIONE VALUES ('"
												+ (String) GraficaHotel.modelClienti
														.getValueAt(rows[i], 3)
												+ "','" + camera + "')");

								JOptionPane.showMessageDialog(null, "Cliente "
										+ clienti[i].getText()
										+ " registrato con successo!");
								this.dispose();
							}
						} catch (HeadlessException | SQLException | IOException e1) {
							e1.printStackTrace();
						}
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}

		}
	}

	private boolean controlloCamera() throws SQLException {
		System.out.println(rs.getString(6) + " in controlloCamera");
		if (rs.getString(6).compareTo("DISPONIBILE") == 0) {
			System.out.println(rs.getString(6)
					+ "in controlloCamera in if disponibile");
			return true;
		} else if (rs.getString(6).compareTo("OCCUPATA") == 0
				&& controlloPosti()) {
			System.out.println(rs.getString(6)
					+ " in controlloCamera in if occupata");
			return true;
		} else if (rs.getString(6) == "PRENOTATA") {

		}
		return false;
	}

	private boolean controlloCliente(String query, int i) throws SQLException,
			HeadlessException, IOException {
		System.out.println("In controllo cliente");
		rs = GraficaHotel.database.stat
				.executeQuery("SELECT * FROM ASSOCIAZIONE where numerodocumento='"
						+ query + "'");
		return rs.next();
	}

	private boolean controlloPosti() throws SQLException {
		if (rs.getInt(3) == rs.getInt(4)) {
			return false;
		}
		return true;
	}

}
